public
class MyThread extends Thread
{
	private int whichThread;
	private int delay;
	private static int account = 0;
	private static Object semaphore;
	public MyThread(int whichThread, int delay)
	{
		super();
		this.delay = delay;
		this.whichThread = whichThread;
	}
	public void run()
	{
		switch(whichThread){
			case 1: thread1();break;
			case 2: thread2();break;
			case 3: thread3();break;
		}
	}
	public static void main(String args[])
	{
		semaphore = new Object();
		new MyThread(1, 1).start();
		new MyThread(2, 2).start();
		new MyThread(3, 0).start();
	}
	public void thread1()
	{
		int temp;
		for(int i = 0; i < 10; i++){
			synchronized(semaphore){
				temp = account;
				try{
					sleep(delay);
				}
				catch(InterruptedException e){
				}
				account = temp + 1;
			}
			System.out.println(getName() + " " + account);
		}
	}
	public void thread2()
	{
		int temp;
		for(int i = 0; i < 10; i++){
			synchronized(semaphore){
				temp = account;
				try{
					sleep(delay);
				}
				catch(InterruptedException e){
				}
				account = temp + 1;
			}
			System.out.println(getName() + " " + account);
		}
	}
	public synchronized void thread3()
	{
		try{
			wait(1000);
		}
		catch(InterruptedException e){
			System.out.println(e);
		}
		System.out.println(getName() + " " + account);
	}
}
